package com.tjcm.tjcminspectmanager;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import cn.emagsoftware.ui.ToastManager;
import cn.emagsoftware.ui.adapterview.BaseLazyLoadAdapter;
import cn.emagsoftware.ui.adapterview.DataHolder;
import cn.emagsoftware.ui.adapterview.ViewHolder;
import cn.emagsoftware.ui.staticmanager.StaticManager;
import cn.emagsoftware.util.LogManager;

import com.tjcm.tjcminspectmanager.manager.NetManager;
import com.tjcm.tjcminspectmanager.model.PlanItem;
import com.tjcm.tjcminspectmanager.model.User;

public class PlanListActivity extends BaseActivity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        if (!LoginActivity.IS_APP_RUNNING)
        {
            StaticManager.restartAppWhenDalvikRecycled(this, LoginActivity.class);
            return;
        }
        final User user = (User) getIntent().getSerializableExtra("USER");
        setContentView(R.layout.planlist);
        ListView list = (ListView) findViewById(R.id.list);
        final View foot = getLayoutInflater().inflate(R.layout.generic_loading, null);
        list.addFooterView(foot, null, false);
        final ProgressBar progress = (ProgressBar) foot.findViewById(R.id.progress);
        final TextView desc = (TextView) foot.findViewById(R.id.desc);
        final Button retry = (Button) foot.findViewById(R.id.retry);
        final BaseLazyLoadAdapter listAdapter = new BaseLazyLoadAdapter(this, new BaseLazyLoadAdapter.LazyLoadCallback()
        {
            @Override
            protected List<DataHolder> onLoad(Object arg0, int arg1, int arg2) throws Exception
            {
                // TODO Auto-generated method stub
                PlanItem[] items = NetManager.queryPlanItems(user.userId, arg2);
                List<DataHolder> holders = new ArrayList<DataHolder>();
                for (PlanItem item : items)
                {
                    holders.add(new DataHolder(item, 0)
                    {
                        @Override
                        public View onCreateView(Context arg0, int arg1, Object arg2)
                        {
                            // TODO Auto-generated method stub
                            View item = LayoutInflater.from(arg0).inflate(R.layout.planlist_item, null);
                            item.setTag(new ViewHolder(item.findViewById(R.id.no), item.findViewById(R.id.text)));
                            onUpdateView(arg0, arg1, item, arg2);
                            return item;
                        }

                        @Override
                        public void onUpdateView(Context arg0, int arg1, View arg2, Object arg3)
                        {
                            // TODO Auto-generated method stub
                            View[] views = ((ViewHolder) arg2.getTag()).getParams();
                            ((TextView) views[0]).setText((arg1 + 1) + "");
                            ((TextView) views[1]).setText(((PlanItem) arg3).planName);
                        }

                        @Override
                        public void onAsyncDataExecuted(Context arg0, int arg1, View arg2, Object arg3, int arg4)
                        {
                            // TODO Auto-generated method stub
                        }
                    });
                }
                return holders;
            }
        })
        {
            @Override
            protected void onBeginLoad(Context arg0, Object arg1)
            {
                // TODO Auto-generated method stub
                foot.setVisibility(View.VISIBLE);
                progress.setVisibility(View.VISIBLE);
                desc.setVisibility(View.VISIBLE);
                retry.setVisibility(View.GONE);
            }

            @Override
            protected void onAfterLoad(Context arg0, Object arg1, Exception arg2)
            {
                // TODO Auto-generated method stub
                if (arg2 == null)
                {
                    foot.setVisibility(View.GONE);
                    int count = getCount();
                    if (count == 0)
                        ToastManager.showLong(arg0, R.string.generic_loading_empty);
                    else
                    {
                        int pageCount = Integer.parseInt(((PlanItem) queryDataHolder(count - 1).getData()).pageCount);
                        if (pageCount >= 0)
                            setPages(pageCount);
                    }
                } else
                {
                    LogManager.logE(PlanListActivity.class, "query plan items failed.", arg2);
                    progress.setVisibility(View.GONE);
                    desc.setVisibility(View.GONE);
                    retry.setVisibility(View.VISIBLE);
                }
            }
        };
        retry.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                // TODO Auto-generated method stub
                listAdapter.load(listAdapter.getCurCondition());
            }
        });
        listAdapter.bindLazyLoading(list, 0);
        list.setAdapter(listAdapter);
        list.setOnItemClickListener(new AdapterView.OnItemClickListener()
        {
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)
            {
                // TODO Auto-generated method stub
                PlanItem item = (PlanItem) listAdapter.queryDataHolder(arg2).getData();
                Intent intent = new Intent(PlanListActivity.this, PlanDetailActivity.class);
                intent.putExtra("PLAN_ITEM", item);
                intent.putExtra("USER", user);
                startActivity(intent);
            }
        });
    }

}